import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

public class BFSSolution {

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int depth = 1;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; ++i) {
                TreeNode node = queue.poll();
                if (node.left == null && node.right == null) {
                    return depth;
                }
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null){
                    queue.offer(node.right);
                }
            }
            ++depth;
        }
        return depth;
    }

    public int openLock(String[] deadends, String target) {
        Set<String> deads = new HashSet<>();
        for (String str : deadends) {
            deads.add(str);
        }
        Queue<String> queue = new LinkedList<>();
        Set<String> visited = new HashSet<>();
        int step = 0;
        queue.offer("0000");
        visited.add("0000");
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                String cur = queue.peek();
                queue.poll();
                if (deads.contains(cur)) {
                    continue;
                }
                if (cur.equals(target))
                    return step;
                for (int j = 0; j < 4; j++) {
                    String plus = plusOne(cur, j);
                    if (!visited.contains(plus)) {
                        queue.offer(plus);
                        visited.add(plus);
                    }
                    String minus = minusOne(cur, j);
                    if (!visited.contains(minus)) {
                        queue.offer(minus);
                        visited.add(minus);
                    }
                }

            }
            ++step;
        }
        return -1;
    }

    private String plusOne(String cur, int j) {
        StringBuilder builder = new StringBuilder(cur);
        if (cur.charAt(j) == '9') {
            builder.setCharAt(j, '0');
        } else {
            builder.setCharAt(j, (char) (cur.charAt(j) + 1));
        }
        return builder.toString();
    }

    private String minusOne(String cur, int j) {
        StringBuilder builder = new StringBuilder(cur);
        if (cur.charAt(j) == '0') {
            builder.setCharAt(j, '9');
        } else {
            builder.setCharAt(j, (char) (cur.charAt(j) - 1));
        }
        return builder.toString();
    }
}

